﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace TLM.Wizard.ViewModels
{
    public class WizardBaseViewModel : ViewModelBase
    {
        private ObservableCollection<BaseViewModel> _wizardPages;
        private int _currentPageNumber;

        #region Command fields
        
        private RelayCommand _cancelCommand;
        private RelayCommand _previousCommand;
        private RelayCommand _nextCommand;

        #endregion

        public WizardBaseViewModel()
        {
            CreateCommands();

            ViewModelLocator locator = new ViewModelLocator();

            _wizardPages = new ObservableCollection<BaseViewModel>();

            _wizardPages.Add(locator.Start);
            _wizardPages.Add(locator.ConversionChoice);
            _wizardPages.Add(locator.ConversionProgress);
            _currentPageNumber = 0;

            MigrationCancelled = false;
        }

        private void CreateCommands()
        {
 	        _cancelCommand = new RelayCommand(CancelMigration);
            _previousCommand = new RelayCommand(GoToPreviousPage, () => IsPreviousPageAvailable);
            _nextCommand = new RelayCommand(GoToNextPage, () => IsNextPageAvailable);
        }

        public BaseViewModel CurrentPage
        {
            get { return _wizardPages[_currentPageNumber]; }
        }

        public int CurrentPageNumber
        {
            get
            {
                return _currentPageNumber;
            }
            set
            {
                if (_currentPageNumber != value)
                {
                    _currentPageNumber = value;
                    RaisePropertyChanged("CurrentPageNumber");
                    RaisePropertyChanged("CurrentPage");
                    
                }
            }
        }

        public string NextButtonText 
        {
            get
            {
                return "Next";
            }
        }

        public string PreviousButtonText
        {
            get
            {
                return "Previous";
            }
        }

        public string CancelButtonText
        {
            get
            {
                return "Cancel";
            }
        }

        public RelayCommand CancelCommand 
        { 
            get 
            {
                return _cancelCommand; 
            }
        }

        public RelayCommand PreviousCommand
        {
            get
            {
                return _previousCommand; 
            }
        }

        public RelayCommand NextCommand
        {
            get
            {
                return _nextCommand; 
            }
        }

        private bool IsPreviousPageAvailable
        {
            get
            {
                return (_currentPageNumber - 1) >= 0;
            }
        }

        private bool IsNextPageAvailable
        {
            get
            {
                return (_currentPageNumber + 1) < _wizardPages.Count;
            }
        }

        public bool MigrationCancelled
        {
            get;
            set;
        }

        private void CancelMigration()
        {
            MigrationCancelled = true;
        }

        private bool CanExecuteTestMethod(bool returnValue)
        {
            return returnValue;
        }

        private void GoToNextPage()
        {
            CurrentPage.PrepareNavigation();
            CurrentPageNumber++;
        }

        private void GoToPreviousPage()
        {
            CurrentPageNumber--;
        }

    }
}
